﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data;
using MySql.Data.MySqlClient;

namespace CSharp_homework9
{
    public partial class Form1 : Form
    {
        bool isChange = true;
        int count = 0;
        //int count1 = 0;
        //MySqlDataAdapter da = new MySqlDataAdapter();
        //DataSet dt = new DataSet();

        public Form1()
        {
            InitializeComponent();
        }


        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private static MySqlConnection GetConnection()
        {
            MySqlConnection connection = new MySqlConnection(
                "datasource=localhost;username=root;" + "password=aaaaaa;database=homework9;charset=utf8"
                );
            connection.Open();
            return connection;
        }



        private void textBox2_TextChanged(object sender, EventArgs e)
        {
            //isChange = true;
        }


        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            
        }

        private void textBox2_KeyDown(object sender, KeyEventArgs e)
        {
            
        }

        private void textBox1_KeyUp(object sender, KeyEventArgs e)
        {
            
        }

        private void textBox2_KeyUp(object sender, KeyEventArgs e)
        {


            using (MySqlConnection conn = GetConnection())
            {
                string stm = "SELECT chineseWord FROM word";
                using (MySqlCommand cmd = new MySqlCommand(stm, conn))
                {
                    DataSet ds = new DataSet();
                    MySqlDataAdapter da = new MySqlDataAdapter();
                    da.SelectCommand = cmd;
                    da.Fill(ds);
                    if (count < 1280 && e.KeyCode == Keys.Enter)
                    {
                        textBox1.Text = ds.Tables[0].Rows[count]["chineseWord"].ToString();
                        //isChange = false;
                        count++;
                    }
                }
            }

            using (MySqlConnection conn = GetConnection())
            {
                string stm = "SELECT englishWord FROM word";
                using (MySqlCommand cmd = new MySqlCommand(stm, conn))
                {
                    DataSet ds = new DataSet();
                    MySqlDataAdapter da = new MySqlDataAdapter();
                    da.SelectCommand = cmd;
                    da.Fill(ds);
                    if (count < 1280 && e.KeyCode == Keys.Enter)       //一共有1280个单词单词
                    {
                        if (count > 1)
                        {
                            //textBox2.Text = "abolish";
                            if (ds.Tables[0].Rows[count-2]["englishWord"].ToString() == textBox2.Text)
                            {
                                MessageBox.Show("正确！");
                            }
                            else
                            {
                                MessageBox.Show("错误！");
                                count--;
                            }
                        }
                        //isChange = false;
                    }
                    //count1++;
                }
            }
        }
    }
}
